#ifndef ENCODER_H
#define ENCODER_H

#include <Arduino.h>

class Encoder {
public:
    Encoder();
    void init();
    float readAngle();                 // Read mechanical angle
    float getElectricalAngle() const;  // Get electrical angle
    float getDefaultOffset() const { return DEFAULT_OFFSET; }  // Get default offset angle
    void updateAngles();               // Update both angles in interrupt
    
private:
    SPI_HandleTypeDef hspi3;
    static constexpr uint8_t DATA_LENGTH = 24;      // 24 bits total
    static constexpr float ANGLE_SCALE = 2.0f * PI / 16384.0f;
    static constexpr uint8_t POLE_PAIRS = 7;        // Number of pole pairs
    // Default offset angle: 138 degrees in radians
    static constexpr float DEFAULT_OFFSET = 138.0f * PI / 180.0f;
    volatile float theta_mech;                       // Mechanical angle in radians
    volatile float theta_elec;                       // Electrical angle in radians
    uint32_t readRawData();
    float normalizeAngle(float angle);              // Normalize angle to 0-2π
};

#endif
